package Servlets;

import Model.JavaSchoolModel;
import PackDatos.GestionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class srv_login extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession(true);
        try {
            //Obtengo los datos del login
            String usuario = request.getParameter("usuario");
            String clave = request.getParameter("clave");
            // Proceso
            JavaSchoolModel model = new JavaSchoolModel();
            String hoy = model.fecha_actual();
            session.setAttribute("fechahoy", hoy);
            GestionBD og = new GestionBD();
            og.conectar();
            String sql = "select id_empleado,emp_nombre from empleado where emp_usuario='" + usuario + "' and emp_clave='" + clave + "'";
            ResultSet rs = og.cargadatos(sql);
            if (rs.next()) {
                int idEmp = rs.getInt(1);
                String nom = rs.getString(2);
                session.setAttribute("idEmpleado", idEmp);
                session.setAttribute("usuario",nom);
                og.cierracon();
                response.sendRedirect("main.jsp");
            } else {
                og.cierracon();
                response.sendRedirect("errorLogin.jsp");
            }
        } catch (Exception ex) {
            out.print(ex.toString());
        }
    }
}